package com.woniuxy.shop.servelet.front;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.woniuxy.shop.entity.Address;
import com.woniuxy.shop.entity.ResponseEntity;
import com.woniuxy.shop.entity.TopAddress;
import com.woniuxy.shop.entity.User;
import com.woniuxy.shop.service.AddressService;
import com.woniuxy.shop.service.ServiceProxyFactory;
import com.woniuxy.shop.service.TopAddressService;
import com.woniuxy.shop.utils.ShopUtils;

/**
 * Servlet implementation class CateGoryServelet
 */
@WebServlet("/usercenter/address")
public class AddressServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	TopAddressService tas = ServiceProxyFactory.getProxy(TopAddressService.class);
	AddressService as = ServiceProxyFactory.getProxy(AddressService.class);
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String opr = request.getParameter("opr");
		if ("add".equals(opr)) {
			doAdd(request, response);
		}else if("showAddress".equals(opr)) {
			doShow(request, response);
		}else if("del".equals(opr)) {
			doDel(request, response);
		}
		else if("setDefault".equals(opr)) {
			doSetDefault(request, response);
		}
	}
	
	private void doSetDefault(HttpServletRequest request, HttpServletResponse response) throws IOException {
		ResponseEntity<Void> rese = new ResponseEntity<Void>();
		int id = Integer.parseInt(request.getParameter("id"));
		try {
			as.setDefault(id);
		} catch (Exception e) {
			rese.setCode(101);
			ShopUtils.write(response, rese);
		}
		rese.setCode(200);
		ShopUtils.write(response, rese);
		
	}

	/**
	 * 删除指定ID的地址信息
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	private void doDel(HttpServletRequest request, HttpServletResponse response) throws IOException {
		ResponseEntity<Void> rese = new ResponseEntity<Void>();
		int id = Integer.parseInt(request.getParameter("id"));
		try {
			as.delete(id);
		} catch (Exception e) {
			rese.setCode(101);
			ShopUtils.write(response, rese);
		}
		rese.setCode(200);
		ShopUtils.write(response, rese);
	}

	/**
	 * 查询用户地址
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	private void doShow(HttpServletRequest request, HttpServletResponse response) throws IOException {
		ResponseEntity<List<Address>> rese = new ResponseEntity<>();
		List<Address> addresses = null;
		try {
			User u = (User)request.getSession().getAttribute("user");
			if(u==null) {
				//没登录就转到提示登录页面
				response.sendRedirect("../usernotlogin.jsp");
				return;
			}
			//根据parentid得到省市区
			addresses = ShopUtils.getAddresses(u.getId());
		} catch (Exception e) {
			rese.setCode(101);
			rese.setMsg("error");
			ShopUtils.write(response, rese);
		}
		rese.setData(addresses);
		rese.setCode(200);
		rese.setMsg("ok");
		ShopUtils.write(response, rese);
		
	}

	/**
	 * 增加地址
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void doAdd(HttpServletRequest request, HttpServletResponse response) throws IOException {
		ResponseEntity<Void> rese = new ResponseEntity<Void>();
		String rname = request.getParameter("rname");
		String pro = request.getParameter("pro");
		String city = request.getParameter("city");
		String area = request.getParameter("area");
		String telphone = request.getParameter("telphone");
		String address = request.getParameter("address");
		String type = request.getParameter("type");
		
		//取得用户id
		User u  = (User)request.getSession().getAttribute("user");
		//获取Adsress里面的TopAddressid
		int topid = 0;
		if(pro!=null&&!pro.trim().equals("")) {
			int proid = addTop(pro, 1);
			if(city!=null&&!city.trim().equals("")) {
				int cityid = addTop(city, proid);
				if(area!=null&&!area.trim().equals("")) {
					topid = addTop(area, cityid);
				}else {
					topid = cityid;
				}
				
			}else {
				topid = proid;
			}
		}else {
			topid = 1;
		}
		
		//创建地址
		Address a = new Address();
		a.setAccepter(rname);
		a.setTopaddress(topid);
		a.setUserid(u.getId());
		a.setDetailArea(address);
		a.setTelephone(telphone);
		if("true".equals(type)) {
			a.setIsDefault("是");
		}else {
			a.setIsDefault("否");
		}
		
		//添加地址
		try {
			as.addAddress(a);;
		} catch (Exception e) {
			System.out.println(e);
		}
		
//		System.out.println(rname);
//		System.out.println(pro);
//		System.out.println(city);
//		System.out.println(area);
//		System.out.println(address);
//		System.out.println(telphone);
//		System.out.println(type);
		
		//数据有效性校验
		
		
		
		rese.setCode(200);
		rese.setMsg("ok");
		ShopUtils.write(response, rese);
		
	}


	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}
	
	public int addTop(String name,int parentid) {
		TopAddress ta = new TopAddress();
		//先查有没有这个省份
		TopAddress tp = tas.getTopAddressByName(name,parentid);
		if(tp==null) {
			ta.setParentid(parentid);
			ta.setName(name);
			tas.add(ta);
			tp = tas.getTopAddressByName(name,parentid);
		}
		return tp.getId();
		
	}

}
